package com.ruoyi.project.comp.comment.controller;

import java.util.List;

import com.ruoyi.project.comp.work.domain.CompWork;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.comp.comment.domain.CompComment;
import com.ruoyi.project.comp.comment.service.ICompCommentService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 作品评论Controller
 * 
 * @author ruoyi
 * @date 2025-11-02
 */
@Controller
@RequestMapping("/comp/comment")
public class CompCommentController extends BaseController
{
    private String prefix = "comp/comment";

    @Autowired
    private ICompCommentService compCommentService;

    @RequiresPermissions("comp:comment:view")
    @GetMapping()
    public String comment()
    {
        return prefix + "/comment";
    }

    /**
     * 查询作品评论列表
     */
    @RequiresPermissions("comp:comment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompComment compComment)
    {
        startPage();
        List<CompComment> list = compCommentService.selectCommentJoinList(compComment);
        return getDataTable(list);
    }

    /**
     * 导出作品评论列表
     */
    @RequiresPermissions("comp:comment:export")
    @Log(title = "作品评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompComment compComment)
    {
        List<CompComment> list = compCommentService.selectCompCommentList(compComment);
        ExcelUtil<CompComment> util = new ExcelUtil<CompComment>(CompComment.class);
        return util.exportExcel(list, "作品评论数据");
    }

    /**
     * 新增作品评论
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存作品评论
     */
    @RequiresPermissions("comp:comment:add")
    @Log(title = "作品评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompComment compComment)
    {
        return toAjax(compCommentService.insertCompComment(compComment));
    }

    /**
     * 修改作品评论
     */
    @RequiresPermissions("comp:comment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CompComment compComment = compCommentService.selectCompCommentById(id);
        mmap.put("compComment", compComment);
        return prefix + "/edit";
    }

    /**
     * 修改保存作品评论
     */
    @RequiresPermissions("comp:comment:edit")
    @Log(title = "作品评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompComment compComment)
    {
        return toAjax(compCommentService.updateCompComment(compComment));
    }

    /**
     * 删除作品评论
     */
    @RequiresPermissions("comp:comment:remove")
    @Log(title = "作品评论", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(compCommentService.deleteCompCommentByIds(ids));
    }




    @PostMapping("/batchPass")
    @ResponseBody
    public AjaxResult batchPass(@RequestParam("ids") String ids) {
        try {
            if (ids == null || ids.trim().isEmpty()) {
                return AjaxResult.error("请选择需要处理的数据");
            }

            String[] idArray = ids.split(",");
            for (int i = 0; i < idArray.length; i++) {
                String id = idArray[i];
                CompComment entity = new CompComment();
                entity.setId(Long.valueOf(id));
                entity.setStatus(1+"");
                compCommentService.updateCompComment(entity);
            }
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            return AjaxResult.error("操作失败：" + e.getMessage());
        }
    }



    @PostMapping("/batchReject")
    @ResponseBody
    public AjaxResult batchReject(@RequestParam("ids") String ids) {
        try {
            if (ids == null || ids.trim().isEmpty()) {
                return AjaxResult.error("请选择需要处理的数据");
            }

            String[] idArray = ids.split(",");
            for (int i = 0; i < idArray.length; i++) {
                String id = idArray[i];
                CompComment entity = new CompComment();
                entity.setId(Long.valueOf(id));
                entity.setStatus(2+"");
                compCommentService.updateCompComment(entity);
            }
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            return AjaxResult.error("操作失败：" + e.getMessage());
        }
    }


    @PostMapping("/batchDel")
    @ResponseBody
    public AjaxResult batchDel(@RequestParam("ids") String ids) {
        try {
            if (ids == null || ids.trim().isEmpty()) {
                return AjaxResult.error("请选择需要处理的数据");
            }

            String[] idArray = ids.split(",");
            for (int i = 0; i < idArray.length; i++) {
                String id = idArray[i];
                compCommentService.deleteCompCommentById(Long.valueOf(id));
            }
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            return AjaxResult.error("操作失败：" + e.getMessage());
        }
    }



}
